package com.itedge.solutionmanager.web.controller.process.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itedge.infrastructure.service.domain.ISearchableEntityService;
import com.itedge.infrastructure.service.process.IProcessService;
import com.itedge.infrastructure.web.controller.process.impl.AbstractProcessEntityController;
import com.itedge.solutionmanager.constants.SolutionManagerConstants;
import com.itedge.solutionmanager.domain.impl.Customer;
import com.itedge.solutionmanager.domain.impl.Employee;
import com.itedge.solutionmanager.domain.dto.CustomerDto;
import com.itedge.solutionmanager.service.domain.ICustomerService;

@RequestMapping("/customers")
@Controller
public class CustomerController extends AbstractProcessEntityController<Customer> {	

    private ICustomerService customerService;
    private ISearchableEntityService<Employee> employeeService;  
    
    @Autowired
    protected CustomerController(ICustomerService customerService, IProcessService<Customer> customerProcessService, 
    		MessageSource messageSource, ISearchableEntityService<Employee> employeeService) {
		super(customerService, customerProcessService, messageSource);
		this.customerService = customerService;
		this.employeeService = employeeService;
	}

	@Override
	public int getDefaultPageSizeForListView() {
		return SolutionManagerConstants.DEFAULT_SIZE_PER_PAGE;
	}
    
	@Override
	protected String getModelKeyForEntity() {
		return "customer";
	}
	
	@Override
	public String getModelKeyForEntitiesList() {
		return "customers";
	}
	
	@Override
	public String getListView() {
		return "customers/list";
	}

	@Override
	protected String getShowView() {
		return "customers/show";
	}

	@Override
	protected void addModelAttributesForShow(Model model) {
		// Nothing to add		
	}

	@RequestMapping(params = "searchForm", method = RequestMethod.GET)
    public String searchForm(Model model) {
        return "customers/search";
    }	
	
	/*
	 * Method used for AJAX search requests on customers, returning data in JSON format
	 */
	@RequestMapping(params = "searchForm", method = RequestMethod.POST, headers = "Accept=application/json")
    public @ResponseBody List<CustomerDto> searchAsyncForCustomers(@RequestParam(value = "searchName") String searchName,
    	    @RequestParam(value = "maxResults", required = false) Integer maxResults, HttpServletRequest request) {		
		Locale currentLocale = request.getLocale();	
		List<CustomerDto> customerDtoList = new ArrayList<CustomerDto>();
        Customer searchCustomer = new Customer();
        searchCustomer.setName(searchName);
        Iterator<Customer> it = customerService.findEntitiesByCriteria(searchCustomer, maxResults).iterator();
        while(it.hasNext()) {
        	customerDtoList.add(new CustomerDto(it.next(), messageSource, currentLocale));
        }
		return customerDtoList;
    }	

	@ModelAttribute("employee")
    public Collection<Employee> populateEmployee() {
        return employeeService.findAllEntities();
    }

}
